const request = require('../../utils/request.js')
const util = require('../../utils/util.js')
Page({
  data: {
    order_list: [],
    noteMaxLen: 300, // 最多放多少字
    isFake: 0
  },
  onLoad: function(options) {
    var that = this;
    var orderId = options.orderId;
    var url = request.URL + '/order/byOrderId'
    request.request({
      url: url,
      data: {
        orderId: orderId
      },
      success: function(res) {
        var order_list = res.data.orderVO;
        for (var i = 0; i < order_list.goodVOS.length; i++) {
          var goodImgArray = order_list.goodVOS[i].goodImgArray;
          var goodImgUrl = util.string2Array(goodImgArray)[0];
          order_list.goodVOS[i].goodImgUrl = goodImgUrl;
          order_list.goodVOS[i].photoList = [];
          order_list.goodVOS[i].flag_description = 5;
          order_list.goodVOS[i].des_text = "非常好";
          order_list.goodVOS[i].commentDes = "";
          order_list.goodVOS[i].noteNowLen = 0; //备注当前字数
          order_list.goodVOS[i].commentImgArray = [];
          order_list.goodVOS[i].commentLevel = 5;
        }
        that.setData({
          order_list: order_list
        })
        console.log(that.data.order_list)
      },
      fail: function() {
        request.showFailInfo()
      }
    })
  },
  // 监听字数
  bindTextAreaChange: function(e) {
    var that = this
    var value = e.detail.value;
    var index = e.currentTarget.dataset.index;
    var order_list = that.data.order_list;
    var len = parseInt(value.length);
    if (len > that.data.noteMaxLen)
      return;
    order_list.goodVOS[index].noteNowLen = len;
    order_list.goodVOS[index].commentDes = value;
    that.setData({
      order_list: order_list
    })

  },
  // 提交清空当前值
  bindSubmit: function() {
    var that = this;
    var order_list = that.data.order_list;
    var orderId = order_list.order.orderId;
    var userId = 1;
    var goodsArr = order_list.goodVOS;
    var commentsArr = new Array();
    var isFake = that.data.isFake;
    var url = request.URL + '/order/comment';
    //goodId，goodName，commentLevel，commentImgArray，commentDes，isFake
    for (var i = 0; i < goodsArr.length; i++) {
      var commentRes = {};
      var goodId = goodsArr[i].goodId;
      var goodName = goodsArr[i].goodName;
      var commentLevel = goodsArr[i].commentLevel;
      var commentImgArray = goodsArr[i].photoList;
      var commentDes = order_list.goodVOS[i].commentDes;
      commentRes.goodId = goodId;
      commentRes.goodName = goodName;
      commentRes.commentLevel = commentLevel;
      commentRes.commentImgArray = JSON.stringify(commentImgArray);
      commentRes.commentDes = commentDes;
      commentsArr.push(commentRes);
    }
    console.log(commentsArr);
    console.log(JSON.stringify(commentsArr));
    request.request({
      url: url,
      method: 'POST',
      header: {
        "Content-Type": "application/json" //x-www-form-urlencoded"
      },
      data: {
        userId: userId,
        orderId: orderId,
        commentResList: commentsArr,
        isFake: isFake
      },
      success: function(res) {
        if (res.statusCode == 200) {
          wx.showToast({
            title: '发布成功',
            icon: 'success',
            duration: 1500,
            mask: false
          })
          wx.navigateBack({
            delta: 1
          })
        }
        console.log(res);

      },
      fail: function() {
        request.showFailInfo()
      }
    })
  },
  /*描述相符*/
  changeColor1: function(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var order_list = that.data.order_list;
    order_list.goodVOS[index].flag_description = 1;
    order_list.goodVOS[index].des_text = "非常差";
    order_list.goodVOS[index].commentLevel = 1;
    that.setData({
      order_list: order_list
    })
    console.log(that.data.order_list)
  },
  changeColor2: function(e) {
    console.log(e)
    var that = this;
    var index = e.currentTarget.dataset.index;
    console.log(index)
    var order_list = that.data.order_list;
    order_list.goodVOS[index].flag_description = 2;
    order_list.goodVOS[index].des_text = "较差";
    order_list.goodVOS[index].commentLevel = 2;
    that.setData({
      order_list: order_list
    })
    console.log(that.data.order_list)
  },
  changeColor3: function(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var order_list = that.data.order_list;
    order_list.goodVOS[index].flag_description = 3;
    order_list.goodVOS[index].des_text = "一般";
    order_list.goodVOS[index].commentLevel = 3;
    that.setData({
      order_list: order_list
    })
    console.log(that.data.order_list)
  },
  changeColor4: function(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var order_list = that.data.order_list;
    order_list.goodVOS[index].flag_description = 4;
    order_list.goodVOS[index].des_text = "较好";
    order_list.goodVOS[index].commentLevel = 4;
    that.setData({
      order_list: order_list
    })
    console.log(that.data.order_list)
  },
  changeColor5: function(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var order_list = that.data.order_list;
    order_list.goodVOS[index].flag_description = 5;
    order_list.goodVOS[index].des_text = "非常好";
    order_list.goodVOS[index].commentLevel = 5;
    that.setData({
      order_list: order_list
    })
    console.log(that.data.order_list)
  },
  checkboxChange: function(e) {
    console.log('checkbox发生change事件，携带value值为：', e.detail.value.length);
    var that = this;
    var isFake = e.detail.value.length;
    that.setData({
      isFake: isFake
    })
  },
  upload_imgs: function(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var order_list = that.data.order_list;
    wx.chooseImage({
      count: 6, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function(res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths
        order_list.goodVOS[index].photoList = tempFilePaths;
        that.setData({
          order_list: order_list
        })
      }
    })
  }
})